﻿using StudentControlSystem.Entity;
using System;
using System.Collections.Generic;
using System.Data.SQLite;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StudentControlSystem.Controller
{
    internal class StudentController
    {
        string connectionString = DBHelper.ConnStrings;

        //新增学生
        public void AddStudent(Student student)
        {
            using (var connection = new SQLiteConnection(connectionString))
            {
                connection.Open();
                var command = new SQLiteCommand("INSERT INTO Students (Name, ClassId) VALUES (@name, @classId)", connection);
                command.Parameters.AddWithValue("@fullName", student.Name);
                command.Parameters.AddWithValue("@classId", student.ClassId);
                command.ExecuteNonQuery();
                Log.LogAction("Added student: " + student.Name);
            }
        }

        //更新学生数据
        public void UpdateStudent(Student student)
        {
            using (var connection = new SQLiteConnection(connectionString))
            {
                connection.Open();
                var command = new SQLiteCommand("UPDATE Students SET Name = @name WHERE StudentId = @id", connection);
                command.Parameters.AddWithValue("@name", student.Name);
                command.Parameters.AddWithValue("@id", student.StudentId);
                command.ExecuteNonQuery();
                Log.LogAction("Updated student: " + student.Name);
            }
        }

        //删除学生
        public void DeleteStudent(int studentId)
        {
            using (var connection = new SQLiteConnection(connectionString))
            {
                connection.Open();
                var command = new SQLiteCommand("DELETE FROM Students WHERE StudentId = @id", connection);
                command.Parameters.AddWithValue("@id", studentId);
                command.ExecuteNonQuery();
                Log.LogAction("Deleted student with ID: " + studentId);
            }
        }

        public List<Student> GetStudentsByClassId(int classId)
        {
            var students = new List<Student>();
            using (var connection = new SQLiteConnection(connectionString))
            {
                connection.Open();
                var command = new SQLiteCommand("SELECT * FROM Students WHERE ClassId = @classId", connection);
                command.Parameters.AddWithValue("@classId", classId);
                using (var reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        students.Add(new Student
                        {
                            StudentId = reader.GetInt32(0),
                            Name = reader.GetString(1),
                            ClassId = reader.GetInt32(2)
                        });
                    }
                }
            }
            return students;
        }

        public Student GetById(int studentId)
        {
            Student student = null;
            using (var connection = new SQLiteConnection(connectionString))
            {
                connection.Open();
                var command = new SQLiteCommand("SELECT * FROM Students WHERE StudentId = @id", connection);
                command.Parameters.AddWithValue("@id", studentId);
                using (var reader = command.ExecuteReader())
                {
                    if (reader.Read())
                    {
                        student = new Student
                        {
                            StudentId = reader.GetInt32(0),
                            Name = reader.GetString(1),
                            ClassId = reader.GetInt32(2)
                        };
                    }
                }
            }
            return student;
        }
    }
}
